/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.apache.log4j.Logger;

/**
 * A window to display the thematic map in full screen
 * It was tricky to make it work correctly with Windows, Linux and Mac 
 * at the same time
 * @author stl
 *
 */
public class JFrameThematicMapFullScreen extends JFrame {
	
    private static Logger logger = Logger.getLogger(JFrameThematicMapFullScreen.class); 

	private JPanel jContentPane = null;
	private JLabel jLabelBitmap = null;
	private Dimension screenDimension =null;

	/**
	 * This is the default constructor
	 */
	public JFrameThematicMapFullScreen() {
		super();
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension dim = toolkit.getScreenSize();
		
		setSize( dim.width, dim.height );
		
		setContentPane( getJContentPane() );
		setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
		setUndecorated( true );
		setAlwaysOnTop( true );
		setExtendedState( Frame.MAXIMIZED_BOTH );
				
		// Does not seem to work with Linux
	    /*GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
	    if ( device.isFullScreenSupported() ) {
	    	device.setFullScreenWindow(this);
	    	screenWidth =jLabelBitmap.getWidth();
	    	screenHeight =jLabelBitmap.getHeight();
	    } else {
	    	setExtendedState(Frame.MAXIMIZED_BOTH);
	    }*/
				
		addWindowListener( new WindowAdapter() {
			public void windowActivated( WindowEvent e ) {					
				Toolkit toolkit = Toolkit.getDefaultToolkit();
				Dimension screenSize = toolkit.getScreenSize();
				logger.debug( "getDefaultToolkit windowActivated width=" + screenSize.width + ", height=" + screenSize.height );
				
				Dimension winSize =((JFrame)e.getComponent()).getSize();
				logger.debug( "jframe.getSize width=" + winSize.width + ", height=" + winSize.height );
				
				screenDimension =new Dimension( Math.min( screenSize.width, winSize.width )
						  					  , Math.min( screenSize.height, winSize.height ) );
				
				/*BufferedImage bi =mtk.retrieveThematicMapImage( screenDimension.width, screenDimension.height );
			    
			    jLabelBitmap.setIcon( new ImageIcon( bi ) );*/
			}
		});				
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		
		if ( jContentPane == null ) {
			jContentPane = new JPanel();
			jContentPane.setLayout( new BorderLayout() );
			
			jLabelBitmap = new JLabel();
			jContentPane.add( jLabelBitmap, BorderLayout.CENTER );
			
			jLabelBitmap.addMouseListener( new MouseAdapter() {
				public void mouseClicked(MouseEvent e) {
					dispose();
				}
			});
			
			jLabelBitmap.setFocusable( true );
			
			jLabelBitmap.addKeyListener( new KeyListener() {

				public void keyPressed(KeyEvent e) {
					dispose();
				}

				public void keyReleased(KeyEvent e) { }

				public void keyTyped(KeyEvent e) { }
				
			});

		}
		
		return jContentPane;
	}
	
	public Dimension getScreenDimension() {
		return screenDimension;
	}
	
	public void setImage( BufferedImage bi ) {
	    jLabelBitmap.setIcon( new ImageIcon( bi ) );
	}

}
